<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="auto"
    >
      <el-form-item label="零件名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入零件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零件号码" style="margin-left:-30px" prop="goodsNo">
        <el-input
          v-model="queryParams.goodsNo"
          placeholder="请输入零件号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次号" style="margin-left:-30px" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" style="margin-left:-30px" prop="supplier">
        <el-select
          style="width:140px"
          v-model="queryParams.supplier"
          clearable
          filterable
          placeholder="请选择供应商"
        >
          <el-option
            v-for="item in supplierList"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <br>
      <el-form-item label="入库日期" prop="inOutDate">
        <el-date-picker
          style="width: 210px"
          v-model="queryParams.inOutDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计划发货日期" prop="planDeliverData">
        <el-date-picker
          style="width: 140px"
          v-model="queryParams.planDeliverData"
          type="date"
          placeholder="选择日期时间"
          clearable
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="系列号" style="margin-left:-50px" prop="parentGoodsNo">
        <el-input
          v-model="queryParams.parentGoodsNo"
          placeholder="请输入系列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作员" style="margin-left:-30px" prop="createBy">
        <el-select
          style="width:130px"
          v-model="queryParams.createBy"
          clearable
          filterable
          placeholder="请选择操作员"
        >
          <el-option
            v-for="(item, index) in filterable"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left:-5px">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tms:warehouseIn:add']"
        >新增入库
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tms:warehouse:export']"
        >导出
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tms:warehouseIn:remove']"
        >删除
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      :border="true"
      v-loading="loading"
      :data="goodsList"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" width="55" align="center" prop="id"/>
      <el-table-column
        label="批次号"
        width="150"
        align="center"
        prop="batchNo"
      />
      <el-table-column label="供应商" width="100" align="center" prop="supplier">
      </el-table-column>
      <el-table-column label="箱号" width="100" align="center" prop="packageNo">
      </el-table-column>
      <el-table-column
        label="包装"
        width="80"
        align="center"
        prop="packageType"
      />
      <el-table-column
        label="计划发货日期"
        width="100"
        align="center"
        prop="planDeliverData"
      />
      <el-table-column
        label="卡特需求日"
        width="120"
        align="center"
        prop="requireDate"
      />
      <el-table-column
        label="订单号"
        width="120"
        align="center"
        prop="orderNo"
      />
      <el-table-column
        label="系列号"
        width="150"
        align="center"
        prop="parentGoodsNo">
        <template slot-scope="scope">
          <router-link :to="'/goods/goodsParent?parentGoodsNo='+ scope.row.parentGoodsNo" class="link-type">
            <span>{{ scope.row.parentGoodsNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
        label="零件号码"
        width="180"
        align="center"
        prop="goodsNo">
        <template slot-scope="scope">
          <router-link :to="'/goods/goods?goodsNo='+ scope.row.goodsNo" class="link-type">
            <span>{{ scope.row.goodsNo }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
        label="零件名称"
        width="150"
        align="center"
        prop="goodsName"
      />
      <!-- prop="stock" -->
      <el-table-column label="入库数量" width="80" align="center">
        <template slot-scope="scope">
          <div class="count">
            <span>+</span>
            <span @dblclick="cancelJinzhi(scope.row.stock)">
              <input
                :disabled="jinzhi"
                type="text"
                class="text"
                style="border: none"
                v-model="scope.row.stock"
                oninput="value=value.replace(/[^\d]/g,'')"
                @blur="txtblur(scope.row.stock, scope.row.id)"
              />
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="单位" width="50" align="center" prop="unit"/>
      <el-table-column
        label="入库日期"
        width="160"
        align="center"
        prop="inOutDate"
      />
      <el-table-column
        label="入库操作员"
        width="100"
        align="center"
        prop="createBy"
      />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[10,20,30,50,500]"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <AddWarehouseIn
      :title="title"
      :dialogVisible="dialogVisible"
      @refresh="refresh"
    ></AddWarehouseIn>
  </div>
</template>

<script>
import {delGoods, getGoods, getPeople, getSupplier, listGoods, updateStock,} from "@/api/tms/inventory";
import AddWarehouseIn from "./components/AddWarehouseIn.vue";

export default {
  dicts: [],
  name: "WarehouseIn",
  components: {
    AddWarehouseIn,
  },
  data() {
    return {
      content: "",
      jinzhi: true,
      // 操作员
      filterable: [],
      supplierList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      num: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        parentGoodsNo: "",
        categoryId: "",
        supplier: "",
        goodsName: "",
        goodsNo: "",
        unit: "",
        model: "",
        grossWeight: "",
        netWeight: "",
        volume: "",
        type: "in",
        inOutDateStart: "",
        inOutDateEnd: "",
        planDeliverData: "",
        inOutDate: [],
      },
      // inOutDate:this.queryParams.inOutDate,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentGoodsNo: [
          {required: true, message: "系列号不能为空", trigger: "blur"},
        ],
        categoryId: [
          {required: true, message: "商品类型不能为空", trigger: "blur"},
        ],
        supplier: [
          {required: true, message: "供应商不能为空", trigger: "blur"},
        ],
        goodsName: [
          {required: true, message: "商品名称不能为空", trigger: "blur"},
        ],
        goodsNo: [
          {required: true, message: "商品唯一编号不能为空", trigger: "blur"},
        ],
        unit: [
          {required: true, message: "商品单位不能为空", trigger: "blur"},
        ],
        model: [
          {required: true, message: "商品型号不能为空", trigger: "blur"},
        ],
        grossWeight: [
          {required: true, message: "毛重(公斤)不能为空", trigger: "blur"},
        ],
        netWeight: [
          {required: true, message: "净重(公斤)不能为空", trigger: "blur"},
        ],
        volume: [
          {required: true, message: "体积(立方米)不能为空", trigger: "blur"},
        ],
        length: [
          {required: true, message: "长(厘米)不能为空", trigger: "blur"},
        ],
        width: [
          {required: true, message: "宽(厘米)不能为空", trigger: "blur"},
        ],
        height: [
          {required: true, message: "高(厘米)不能为空", trigger: "blur"},
        ],
        currentStock: [
          {required: true, message: "当前库存不能为空", trigger: "blur"},
        ],
        createBy: [
          {required: true, message: "创建者不能为空", trigger: "blur"},
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"},
        ],
        updateBy: [
          {required: true, message: "更新者不能为空", trigger: "blur"},
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"},
        ],
        remark: [{required: true, message: "备注不能为空", trigger: "blur"}],
        deleted: [
          {
            required: true,
            message: "删除状态 0:未删除 1:已删除不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    'queryParams.inOutDate'(newVal) {
      // console.log(newVal);
      if (newVal == null) {
        this.queryParams.inOutDate = []
      }
    }
  },
  methods: {
    cancelJinzhi(row) {
      this.content = row;
      this.jinzhi = false;
    },
    txtblur(stock, id) {
      this.jinzhi = true;
      let content = "";
      this.goodsList.map((item) => {
        if (item.id == id) {
          content = item;
        }
      });
      if (this.content != content.stock) {
        let params = {
          stock: parseInt(content.stock),
          id: content.id,
        };
        this.$modal
          .confirm(
            `是否确认修改零件号码为${content.goodsNo}的入库数量为${content.stock}？`
          )
          .then(() => {
            updateStock(params).then((res) => {
              if (res.code == 200) {
                this.getList();
                this.$message.success("修改成功");
              }
            });
          });
      }
    },
    getGoodsList(data) {
      listGoods(data).then((response) => {
        this.goodsList = response.rows;
        for (let i = 0; i < this.goodsList.length; i++) {
          this.goodsList[i].stock = `${this.goodsList[i].stock}`;
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询商品列表列表 */
    getList() {
      getSupplier().then((res) => {
        this.supplierList = res.data;
      });
      getPeople().then((res) => {
        this.filterable = res.data;
      });
      if (this.queryParams.inOutDate.length > 0) {
        let data = {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,
          inOutDateStart: `${this.queryParams.inOutDate[0]} 00:00:00`,
          inOutDateEnd: `${this.queryParams.inOutDate[1]} 23:59:59`,
          goodsName: this.queryParams.goodsName,
          goodsNo: this.queryParams.goodsNo,
          parentGoodsNo: this.queryParams.parentGoodsNo,
          supplier: this.queryParams.supplier,
          createBy: this.queryParams.createBy,
          planDeliverData: this.queryParams.planDeliverData,
          batchNo:this.queryParams.batchNo,
          type:this.queryParams.type,
        };
        this.getGoodsList(data);
      } else {
        this.getGoodsList(this.queryParams);
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentGoodsNo: null,
        categoryId: null,
        supplier: null,
        goodsName: null,
        goodsNo: null,
        unit: null,
        model: null,
        grossWeight: null,
        netWeight: null,
        volume: null,
        length: null,
        width: null,
        height: null,
        currentStock: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deleted: null,
        inOutDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      // this.num = selection.map((res) => parseInt(res.stock.slice(1)));
      this.num = selection.map((item) => parseInt(item.stock));
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 表单隔行变色
    tableRowClassName({row, rowIndex}) {
      if (rowIndex % 2 == 1) {
        return "success-row";
      } else {
        return "warning-row";
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.dialogVisible = true;
      this.title = "新增入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGoods(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品列表";
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let num = 0;
      for (let i = 0; i < this.num.length; i++) {
        num += this.num[i];
      }
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除商品列表编号为"' +
          ids +
          '"的数据项？删除后总库数量将减少' +
          num
        )
        .then(function () {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "tms/goodsStockHistory/export",
        {
          ...this.queryParams,
        },
        `入库明细导出_${new Date().getTime()}.xlsx`
      );
    },
    /** 刷新 */
    refresh() {
      this.dialogVisible = false;
      this.getList();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-table .warning-row {
  background: #f7faff;
}

.el-table .success-row {
  background: #ebf1fb;
}

.text:focus {
  // outline: 1px solid #000;
  background: #fff;
}

.text {
  width: 30px;
  height: 100%;
  background: transparent;
}

.count {
  width: 69px;
  text-align: center;
  display: flex;
  justify-content: center;
}
</style>
